<?php

namespace log\driver;

use app\common\model\LogApp;
use think\contract\LogHandlerInterface;
use think\facade\Config;

class DbLogger extends \ShidianUtil\Thinkphp\Log\Driver\DbLogger implements LogHandlerInterface
{
    protected function write(array $info)
    {
        if (empty($info)) {
            return;
        }

        if ($this->app->request->isCli()) {
            LogApp::insertBatch($info);
        } else {
            $this->info = array_merge($this->info, $info);
        }
    }

    public function __destruct()
    {
        if (!$this->app->request->isCli()) {
            $black_list = Config::get('logrequest.black_list', []);
            if (in_array($this->app->request->pathinfo(), $black_list)) {
                return;
            }
        }

        if (!empty($this->info)) {
            LogApp::insertBatch($this->info);
        }
    }
}
